package com.fand.platform.configuration.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.configuration.dto.LanguageDto;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.platform.configuration.validator.LanguageValidator;
import com.fand.platform.configuration.vo.LanguageVo;
import com.fand.platform.configuration.service.ConfigurationService;
import com.google.common.collect.Maps;

/**
 * 语言
 *
 * @author yaoHT
 */
@Controller
public class LanguageController {
	@Qualifier(ConfigurationService.BEAN_ID)
	@Autowired(required = false)
	private ConfigurationService configurationService;

	@Qualifier(LanguageValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator languageValidator;

	@RequestMapping(value = { "/admin/configuration/language/language/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminLanguageDelete(final LanguageVo languageVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.configurationService.getLanguageService().deleteLanguageByIds(languageVo.getLanguage());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/language/language/edit.html" }, method = RequestMethod.GET)
	public String adminLanguageEdit(final LanguageVo languageVo, final Model model) {
		final LanguageDto language = this.configurationService.getLanguageService().
				getLanguageById(languageVo.getLanguage());

		model.addAttribute("language", language);

		return "/admin/configuration/language/language/edit";
	}

	@RequestMapping(value = { "/admin/configuration/language/language.html", "/admin/configuration/language/language/index.html" })
	public String adminLanguageIndex() {
		return "/admin/configuration/language/language/index";
	}

	@RequestMapping(value = { "/admin/configuration/language/language/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminLanguageIndexPagination(final LanguageVo languageVo) {
		final PaginationDto<Map<String, Object>> languagePagination = this.configurationService.getLanguageService()
				.paginationLanguageBy1ForMapList(languageVo.getLanguage());

		final Map<String, Object> languagePaginationMap = Maps.newHashMap();

		languagePaginationMap.put("total", languagePagination.getTotalSize());
		languagePaginationMap.put("rows", languagePagination.getDataList());

		return languagePaginationMap;
	}

	@RequestMapping(value = { "/admin/configuration/language/language/new.html" }, method = RequestMethod.GET)
	public String adminLanguageNew(final LanguageVo languageVo, final Model model) {
		model.addAttribute("language", languageVo.getLanguage());

		return "/admin/configuration/language/language/new";
	}

	@RequestMapping(value = { "/admin/configuration/language/language/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminLanguageSave(final LanguageVo languageVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.languageValidator.validate(languageVo.getLanguage(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.configurationService.getLanguageService().saveLanguageByIdOrCodeForLanguage(languageVo.getLanguage());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/configuration/language/language/validationBy1.json" })
	public @ResponseBody
	Boolean adminLanguageValidationBy1(final LanguageVo languageVo) {
		try {
			final int count = this.configurationService.getLanguageService().readLanguageByCodeNqIdForInteger(
					languageVo.getLanguage());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/configuration/language/language/view.html" }, method = RequestMethod.GET)
	public String adminLanguageView(final LanguageVo languageVo, final Model model) {
		final LanguageDto language = this.configurationService.getLanguageService().
					getLanguageById(languageVo.getLanguage());

		model.addAttribute("language", language);

		return "/admin/configuration/language/language/view";
	}
}
